import { Autoplainning } from "Autoplanning/Autoplainning";
import { priority } from "utils/constants/definetypes";
import { generateUniqueId } from "utils/utils";

 
export default 
{
    /* 
    triggerEvent("E55S2",{type:"LevelUp",region:"E55S2",room:"E55S2"})


    Game.rooms["E8S5"].getEntitys("OutMineEntity").filter(e=>e.roomName=="E7S6").map(e=>e.getNodeMemory())
    */
    triggerEvent<T extends TEventTypes>(region:EventRegion,eventtype:T,param:IWorldEvents[T])
    {
        (global.GameLoop as any).worldstate.eventTriger(region,eventtype,param);
    },
    addTask(task:any,region:string,isunique=false)
    {
        // global.Log.Info(`添加任务 ${JSON.stringify(task)}`);
        (global.GameLoop as any).decision.addTask("",[task],region,isunique);
        return true;
    },
    canExecute(time: number): boolean
    {
        return Game.time%time==0
    },
    whiteList:{
         
    },
   
    pc:{
        enableRoom(room:string,pcName:string)
        {
            if(Game.powerCreeps[pcName])
            {
                Game.powerCreeps[pcName].memory.OwnerRoom = room;
                if(!Game.rooms[room].memory.powerCreeps)
                {
                    Game.rooms[room].memory.powerCreeps=[pcName]
                }
                else
                {
                    if(!Game.rooms[room].memory.powerCreeps.includes(pcName))
                    {
                        Game.rooms[room].memory.powerCreeps.push(pcName);
                        return `${room}添加PC [${pcName}]成功`
                    }
                }
            }
            return `${room}添加PC [${pcName}]失败` 
        }
    },
    room:{
        clearSpawnTask(roomName:string)
        {
            
            Game.rooms[roomName].getEntity("SpawnEntity").spawnQueue.clear();
        }
    },
    plan:
    {
        set(roomName:string)
        {
            Autoplainning.createPlan(roomName);
            // 同时清理能量的road缓存
            // 同时更新蓝图缓存
            const plan = global.world.PollBlueplan(roomName,true);
            
            // 同时触发添加新房间的事件
        },
        rebuildPlan(roomName:string)
        {
            if(Autoplainning.rebuildPlan(roomName))
            {
                const outMines = Game.rooms[roomName].getEntitys("OutMineEntity");

                outMines.forEach(e=>{
                    
                    (global.test as any).removeRemoteMine(roomName,e.roomName)

                })

                global.entityDB.clear(roomName);
                Game.rooms[roomName].memory={} as any;
                (global.world as any).RegisterRoom(roomName);
            }
        },
        /**
         * 放置建筑
         * @param roomName 
         * @param level 目标等级 L。确定等级时，放置 L-1 -> L .L未定义时 0->L 
         */
        placePlan(roomName:string,level?:number)
        {
            const plan = global.world.PollBlueplan(roomName);
            if(plan)
            {
                const planesite = plan.getNormalPlan();
                const room = Game.rooms[roomName];
                let curlevel = room.controller?.level as number;
                let prelevel = 1;
                if(level)
                {
                    curlevel = level;
                    prelevel = level-1
                }
                const curindex =  planesite.SiteQueue.levelLimit[curlevel];
                const preindex =  planesite.SiteQueue.levelLimit[prelevel];
            
                global.Log.Info(` index :${ preindex}  -> ${curindex}`);
                const thisplans:IPlanItem[] =planesite.SiteQueue.items.slice(preindex,curindex);
               
                const task :ITaskDeclare["room"]={
                    id:generateUniqueId(),
                    priority:priority.S,
                    ttl:5000,
                    type:"room",
                    data:{
                        type:"placePlan",
                        arg:{
                            roomName:room.name,
                            plan:thisplans
                        },
                    },
                    shard:0,
                };
                global.addTask(task,roomName,false);
            }
           
        }
    },
    lab:
    {
        addReaction(roomName:string,target:MineralBoostConstant,amount:number)
        {
            const task :ITaskDeclare["react"]={
                id:generateUniqueId(),
                priority:priority.S,
                ttl:5000,
                type:"react",
                data:{
                    type:"react",
                    arg:{
                         RectionTarget:target,
                         TargetCount:amount,
                         Reactions:[],
                         State:-1,

                    },
                },
                shard:0,
            };
            global.addTask(task,roomName,false);
        },
        globalReaction(target:MineralBoostConstant,amount:number)
        {
            for(let k in Game.rooms)
            {
                const room = Game.rooms[k];
                if(room.getType() ==1 && room.controller!.level>=6)
                {
                    room.memory.labTarget={target:target,number:amount};
                }
            }
        }
    }
}